package cn.edu.nwsuaf.examcenter.service.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import cn.edu.nwsuaf.examcenter.model.PageModel;
import cn.edu.nwsuaf.examcenter.pojo.Teacher;
import cn.edu.nwsuaf.examcenter.service.TeacherService;

@Service(value="teacherService")
public class TeacherServiceImpl extends BaseServiceImpl<Teacher> implements TeacherService {

	@Override
	public void loadTeacher(ModelMap modelMap) {
		// TODO Auto-generated method stub
		PageModel<Teacher> pageModel = new PageModel<>();
		List<Teacher> list = super.findAll(Teacher.class);
		pageModel.setTotalrows(list.size());
		pageModel.setList(list);
		modelMap.addAttribute("pageModel",pageModel);
	}

	@Override
	public String addTeacher(Teacher teacher) {
		// TODO Auto-generated method stub
		String teacherPwd = teacher.getTeacherIdcard().substring(12, 18);
		teacher.setTeacherPassword(teacherPwd);
		super.saveData(teacher);
		return "success";
	}

	@Override
	public String deleteTeacher(String teacherNo) {
		// TODO Auto-generated method stub
		super.deleteData(super.loadById(Teacher.class, teacherNo));
		return "success";
	}

	@Override
	public String deleteTeacherList(String array) {
		// TODO Auto-generated method stub
		String[] teacherNos = array.split(",");
		List<Teacher> list = new ArrayList<>();
		for(String teacherNo: teacherNos){
			list.add(super.loadById(Teacher.class, teacherNo));
		}
		super.deleteDataList(list);
		return "success";
	}

	@Override
	public List<Teacher> jsonLoadTeacherList(String collegeNo) {
		// TODO Auto-generated method stub
		
		String[] param = {collegeNo};
		return super.findAll("FROM Teacher as teacher where teacher.college.collegeNo=?", param);
	}

	@Override
	public Teacher jsonLoadTeacher(String teacherNo) {
		// TODO Auto-generated method stub
		Teacher teacher = super.loadById(Teacher.class, teacherNo);
		Hibernate.initialize(teacher.getCollege());
		return teacher;
	}

	@Override
	public List<Teacher> jsonSearchTeacher(String collegeNo, String teacherName) {
		// TODO Auto-generated method stub
		
		String hql = "FROM Teacher as teacher";
		List<String> keyList = new ArrayList<>();
		List<Teacher> list = new ArrayList<>();
		if(collegeNo!=null && teacherName!=null){
			keyList.add(collegeNo);
			keyList.add("%"+teacherName+"%");
			hql += " where teacher.college.collegeNo=? AND teacher.teacherName like(?)";
		}else if(collegeNo!=null && teacherName==null){
			keyList.add(collegeNo);
			hql += " where teacher.college.collegeNo=?";
		}else if(collegeNo==null && teacherName!=null){
			keyList.add("%"+teacherName+"%");
			hql += " where teacher.teacherName like(?)";
		}
		if(keyList.size()==0){
			list = super.findAll(Teacher.class);
		}else{
			String[] param = keyList.toArray(new String[keyList.size()]);
			list = super.findAll(hql, param);
		}
		for(Teacher t: list){
			Hibernate.initialize(t.getCollege());
		}
		return list;
	}
	
}
